package com.web.rss.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

public class Display extends JFrame implements ActionListener, ItemListener, ListSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8978139201506793071L;
	MainMenuBar menu;
	private Dimension minDim;
	FeedListScrollPane feedListPane;
	FeedContentScrollPane feedContentPane;
	FeedArticleScrollPane feedArticlePane;
	SyndFeed currentFeed;
	SyndFeedInput feedInput;
	
	public Display(String langcode, Dimension dim, HashMap<String,URL> list) {
		this.setSize(dim);
		
		feedInput = new SyndFeedInput();
		
		this.setMinimumSize(new Dimension(50,50));
		menu = new MainMenuBar(langcode);
		setLayout(new BorderLayout());
		minDim = new Dimension(10,10);
		
		/*
		JTextArea feedListArea = new JTextArea();
		feedListArea.setEditable(false);
		JScrollPane feedListPane = new JScrollPane();
		feedListPane.setPreferredSize(new Dimension(240,420));
		feedListPane.setMinimumSize(minDim);
		*/
		
		feedListPane = new FeedListScrollPane(list);
		feedListPane.setPreferredSize(new Dimension(240,420));
		feedListPane.setMinimumSize(minDim);
		feedListPane.displayList.addListSelectionListener(this);
		
		HashMap<String, SyndEntryImpl> Content = new HashMap<String, SyndEntryImpl>();
		Content = getSelectedFeedContent();
		
		/*JTextArea feedContentArea = new JTextArea();
		feedContentArea.setEditable(false);
		JScrollPane feedContentPane = new JScrollPane();
		*/
		feedContentPane = new FeedContentScrollPane(Content);
		feedContentPane.setPreferredSize(new Dimension(410,150));
		feedContentPane.setMinimumSize(minDim);
		feedContentPane.feedEntryList.addListSelectionListener(this);
		
		/*
		JTextArea feedArticleArea = new JTextArea();
		feedArticleArea.setEditable(false);
		JScrollPane feedArticlePane = new JScrollPane();
		*/
		feedArticlePane = new FeedArticleScrollPane(feedContentPane.getSelectedEntry());
		feedArticlePane.setPreferredSize(new Dimension(410,260));
		feedArticlePane.setMinimumSize(minDim);
		
		
		JSplitPane verSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
											feedContentPane,
											feedArticlePane);
		verSplit.setOneTouchExpandable(true);
		verSplit.setResizeWeight(0.4);
		
		JSplitPane horSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
											feedListPane,
											verSplit);
		horSplit.setOneTouchExpandable(true);
		horSplit.setResizeWeight(0.3);
		
		this.add(horSplit);
		//this.add(verSplit);
		menu.fluxAddItem.addActionListener(this);
		menu.helpAboutItem.addActionListener(this);
		this.setJMenuBar(menu);

		
		this.addWindowListener(new windowListener());
	}
	
	@SuppressWarnings("unchecked")
	private HashMap<String, SyndEntryImpl> getSelectedFeedContent() {
		HashMap<String, SyndEntryImpl> tmp = new HashMap<String, SyndEntryImpl>();
		URL localFeedURL = this.feedListPane.getSelectedFeedURL();
		SyndFeed feed;
		List<SyndEntryImpl> entries;
		Iterator<SyndEntryImpl> iter;
		try {
			feed = feedInput.build(new XmlReader(localFeedURL));
			entries = feed.getEntries();
			iter = entries.iterator();
			SyndEntryImpl curr;
			while (iter.hasNext()) {
				curr = iter.next();
				tmp.put(curr.getTitle(), curr);
			}
			
		}
		catch (MalformedURLException mURLex) {
		}
		catch (IOException ioex) {
		}
		catch (FeedException fex) {
		}
		return tmp;
	}
	
	private SyndEntryImpl getSelectedArticleContents() {
		SyndEntryImpl entry;
		entry = this.feedContentPane.getSelectedEntry();
		
		return entry;
	}
	
	public void actionPerformed(ActionEvent ev) {
		// TODO Définir complètement le menu.
		Object source = ev.getSource();
		if (source == this.menu.fluxAddItem) {
			// TODO implémenter la boîte de dialogue et les actions associées
			System.out.println("Clicked on \"Add Flux\"");
		}
		if (source == this.menu.helpAboutItem) {
			// TODO Implémenter la boîte d'affichage "À Propos"
			System.out.println("stajReader version 0.0.1\n");
		}
		
	}
	
	public void itemStateChanged(ItemEvent ev) {
		
	}
	
	class windowListener extends WindowAdapter {
		public void windowClosing (WindowEvent wEv) {
			Display.this.setVisible(false);
			Display.this.dispose();
			System.exit(0);
		}
	}

	public void valueChanged(ListSelectionEvent ev) {
		// TODO Auto-generated method stub
		Object source = ev.getSource();
		
		if (source == feedListPane.displayList) {
			//System.out.println("Selected feed changed : " + this.feedListPane.getSelectedFeed());
			this.feedContentPane.updateContent(this.getSelectedFeedContent());
			this.feedArticlePane.updateDisplay(this.feedContentPane.getSelectedEntry());
		}
		else {
			if ((source == feedContentPane.feedEntryList) && (!(ev.getValueIsAdjusting()))) {
				//System.out.println("Selected article changed : " + feedContentPane.getSelectedEntry().getContents());
				this.feedArticlePane.updateDisplay(this.feedContentPane.getSelectedEntry());
			}
		}
	}
}